<template>
  <div class="box">
    <div class="header">
      <HeaderPoper name="购物车"></HeaderPoper>
    </div>
    <div class="login" v-if="flag">
      登录后可同步账户购物车中的商品
      <van-button type="danger" size="small" to="login">登录</van-button>
    </div>
    <div class="map" v-else>
      <div class="map_left">
        <div class="map_icon"></div>
        <div class="map_text">{{ address[0].area }}</div>
      </div>
      <div class="map_right">
        <div class="right_icon"></div>
        <div class="right_text">编辑商品</div>
      </div>
    </div>

    <div class="main">
      <div class="empty_cart">
        <div class="shopcart_empty_wrap" v-if="cartFlag">
          <div class="empty_icon"></div>
          <p class="empty_txt" v-if="flag">登录后可同步购物车中商品</p>
          <p class="empty_txt" v-else>购物车中空空如也去逛逛吧</p>
        </div>
        <div id="cate_wrap" v-else>
          <div class="cate_main"> 
                       
            <div class="cate_item" v-for="item in cartList" :key="item.tpl"> 
              <van-checkbox-group v-model="checked" ref="checkboxGroup">
                                                    
                <van-checkbox @click="toggleAll" :name="item.id">{{ item.name }}</van-checkbox>
                <van-checkbox @click="toggleAll" :name="item.id"></van-checkbox>
              </van-checkbox-group>
              <van-card
              
                :price="item.price"
                :desc="item.name.substring(0, 3) + '...'"
                :title="item.name.substring(0, 7) + '...'"
                :thumb="item.imgprefix + item.imgbase"
              >
                <template #footer>
                  <div class="footer">
                    <div class="footer_top">
                      <span @click="onSub(item)">-</span>
                      <input type="text" :value="+item.type + 1" />
                      <span @click="onAdd(item)">+</span>
                    </div>
                    <div class="footer_bot">
                      <span class="foot">收藏</span>
                      <span class="foot">删除</span>
                    </div>
                  </div>
                </template>
              </van-card>
            </div>
            <div class="selec">
              <van-checkbox v-model="All" @click="checkAll">全选</van-checkbox>
              <div class="selec_right">
                <div class="total">
                  已选{{ total }}件,合计：
                  <span>￥{{ num }}</span>
                </div>
                <div class="jie">
                  <van-button to="orders" color="linear-gradient(to right, #ff6034, #ee0a24)">
                    去结算
                  </van-button>
                </div>
              </div>
            </div>
          </div>
          <div class="jumpJdApp">
            <img
              src="https://img12.360buyimg.com/img/s694x148_jfs/t1/171999/12/22113/58222/61bad82eE0dcf6d31/d92d1e829bf953da.png"
              alt=""
            />
          </div>
        </div>

        <!-- 猜你喜欢 -->
        <div class="guess">
          <div class="title">
            <div class="line"></div>
            <div class="text">可能你还想要</div>
            <div class="line"></div>
          </div>
          <FilmsShow></FilmsShow>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useContentStore } from '@/stores/content'
import HeaderPoper from './content/components/header-popover.vue'
import FilmsShow from './films/components/films-show.vue'
import { useCarStore } from '@/stores/car'
import { useGoodsStore } from '@/stores/goods'
import { useAddressStore } from '@/stores/address'

const addr = useAddressStore()

const content = useContentStore()
const cart = useCarStore()
const list = useGoodsStore()
const token = content.token
addr.getCities(token as string)
cart.renewCarContent(token as string)
const checked = ref([])
const All = ref(false)
const checkboxGroup = ref(null)
if (!localStorage.getItem('cart')) {
  list.getGoodsList()
}
function onAdd(item) {
  item.type++
}
function onSub(item) {
  if (item.type) {
    item.type--
  }
}
const checkAll = () => {
  if (All.value) {
    //@ts-ignore
    checked.value = arr.value
  } else {
    //@ts-ignore
    checked.value = []
  }
}
const toggleAll = () => {
  //@ts-ignore
  checkboxGroup.value.forEach((item) => {})
}
//总计
const total = computed(() => {
  let num = 0
  checked.value.forEach((ele) => {
    cartList.value?.filter((item) => item.id === ele).forEach((i) => (num += i.type - 0 + 1))
  })
  return num
})
const num = computed(() => {
  let number = 0
  checked.value.forEach((ele) => {
    cartList.value?.filter((item) => item.id === ele).forEach((i) => (number += i.price - 0))
  })
  return (number * total.value).toFixed(2)
})
const flag = computed(() => {
  if (content.token && content.token.length === 32) {
    return false
  }
  return true
})
const cartFlag = computed(() => {
  if (cart.carContent) {
    return false
  }
  return true
})
const cartList = computed(() => {
  let arr: Array<[]> = []
  list.goodslist?.find((item) => {
    //@ts-ignore
    cart.carContent?.forEach((ele) => {
      if (ele.goodsId === item.id) {
        //@ts-ignore
        arr.push(item)
      }
    })
  })
  return arr
})
const arr = computed(() => {
  let Arr = []
  cartList.value.forEach((ele) => {
    Arr.push(ele.id)
  })
  return Arr
})
const address = computed(() => {
  if (localStorage.getItem('address')) {
    return JSON.parse(localStorage.getItem('address') as string)
  } else {
    return addr.defaultId
  }
})
watch(cartList, () => {
  localStorage.setItem('cartList', JSON.stringify(cartList.value))
  //@ts-ignore
})
watch(num, () => {
  cart.carTotal = num.value

})
</script>

<style scoped>
::v-deep(.van-card__content) {
  width: 80px;
  height: 100px;
}
::v-deep(.van-card__title) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.selec {
  background: hsla(0, 0%, 100%, 0.95);
  display: flex;
  height: 50px;
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 50px;
  padding: 10px;
  box-sizing: border-box;
}

.total {
  font-size: 12px;
  position: relative;
}
.selec_right {
  margin-left: 40px;
  line-height: 30px;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  position: relative;
}
.jie {
  position: absolute;
  right: -175px;
  top: -8px;
}
.jie button {
  border-radius: 26px;
  width: 113px;
}
.selec_right span {
  font-size: 16px;
  color: red;
  position: absolute;
  left: 73px;
}
.box {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.header {
  height: 44px;
  border-bottom: 3px solid #ccc;
  background-color: #fff;
}
.map {
  height: 34px;
  background: #fff;
  border-radius: 0 0 20px 20px;
  color: #262626;
  display: flex;
  font-size: 14px;
  line-height: 34px;
  position: relative;
  padding: 0 18px;
}
.map_left {
  width: 60%;
  position: relative;
  padding-left: 18px;
}
.map_right {
  flex: 1;
  position: relative;
}
.right_icon {
  background: #bfbfbf;
  content: '';
  height: 13px;
  margin-top: -0.25rem;
  position: absolute;
  right: 81px;
  top: 50%;
  width: 1px;
}
.right_text {
  float: right;
}
.map_left .map_icon:before {
  background: url(//jstatic.3.cn/static/icon_location_jd@2x.f757de00.png);
  background-size: 12px 12px;
  content: '';
  height: 12px;
  left: 0;
  margin-top: -8px;
  position: absolute;
  top: 50%;
  width: 12px;
}

.main {
  flex: 1;
  background-color: #f2f2f2;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.cate_item {
  height: 173px;
  width: 100%;
  background-color: #fff;
  display: flex;
  position: relative;
  padding: 10px;
  border-radius: 0.5rem;
  margin-top: 0.3rem;
  box-sizing: border-box;
}
.footer {
  display: flex;
  flex-direction: column;
}
.footer_top {
  position: relative;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-around;
  right: -70px;
  bottom: -40px;
}
.footer_bot {
  position: relative;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-around;
  right: -70px;
  bottom: -63px;
}
.foot {
  height: 20px;
  line-height: 20px;
  text-align: center;
}
.footer_top > input {
  width: 25px;
  height: 20px;
  border: none;
  background-color: #ccc;
  margin: 0 5px;
  text-align: center;
  line-height: 20px;
}
.footer_top > span {
  width: 20px;
  height: 20px;
  line-height: 20px;
  font-size: 20px;
  text-align: center;
}

::v-deep(.van-card) {
  display: flex;
  position: relative;
}
::v-deep(.van-card__thumb) {
  width: 100px;
  height: 100px;
}
::v-deep(.van-image__img) {
  width: 100%;
  height: 100%;
}
::v-deep(.van-card__bottom) {
  position: absolute;
  left: 0;
  bottom: 38px;
}
::v-deep(.van-card__price) {
  color: red;
  font-size: 16px;
}
::v-deep(.van-checkbox__icon) {
  height: 68px;
}
::v-deep(.van-checkbox) {
  align-items: normal;
}
::v-deep(.van-checkbox__label) {
  font-size: 12px;
}
::v-deep(.cate_item .van-checkbox__label) {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 180px;
}
.title {
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  padding: 0 0.5rem;
}
.line {
  -webkit-box-flex: 1;
  -webkit-flex: 1 1 auto;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  height: 0.05rem;
  background-color: #ccc;
}
.text {
  -webkit-box-flex: 0;
  -webkit-flex: 0 1 auto;
  -ms-flex: 0 1 auto;
  flex: 0 1 auto;
  position: relative;
  padding: 0 0.75rem;
  height: 1.5rem;
  line-height: 1.5rem;
  color: #999;
  font-size: 16px;
}
.login {
  height: 60px;
  background-color: #fff;
  border-radius: 0px 0px 20px 20px;
  color: #333;
  font-size: 14px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  position: relative;
  padding-right: 50px;
}
img {
  width: 100%;
  height: 100%;
}
.cate_wrap {
  display: flex;
  flex-direction: column;
  padding: 10px;
}
::v-deep(.shopcart_empty_wrap) {
  padding: 70px 0 65px;
  text-align: center;
}
.cate_wrap .cate_main {
  background-color: #fff;
}
::v-deep(.van-card) {
  position: absolute;
  background-color: #fff;
  top: 50px;
  left: 32px;
  padding: 0;
  border: none;
}
.jumpJdApp {
  padding: 15px 20px 13px 8px;
}
::v-deep(.van-nav-bar__title) {
  font-size: 16px;
  line-height: 44px;
  text-align: center;
  color: #333;
  overflow: hidden;
  font-weight: 400;
}
::v-deep(.shopcart_empty_wrap .empty_txt) {
  color: #333;
  font-size: 16px;
  line-height: 16px;
  margin-top: 15px;
}
::v-deep(.shopcart_empty_wrap .empty_icon) {
  background: url(//img11.360buyimg.com/jdphoto/s180x180_jfs/t18163/292/540553659/74408/adeb7463/5a93c51cN3bb5e37b.png)
    no-repeat 50%;
  background-size: 90px 90px;
  display: inline-block;
  height: 90px;
  width: 90px;
}
::v-deep(.van-button--small) {
  border-radius: 15px;
  box-shadow: 0 3px 6px 0 rgba(255, 65, 66, 0.2);
  color: #fff;
  display: inline-block;
  height: 30px;
  line-height: 30px;
  margin-left: 10px;
  text-align: center;
  width: 58px;
  margin-bottom: 10px;
  position: absolute;
  top: 15px;
  right: 30px;
  font-size: 14px;
}
</style>
